﻿Imports System.Data.SqlClient
Imports System.Configuration
Imports MySQL
Imports MySQLCommand

Public Class frmXuatKho

    Private Sub frmXuatKho_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'clsSQL.setConnection(ConfigurationSettings.AppSettings("connect"))
        'If clsSQL.Connected() = False Then
        '    MsgBox("Lỗi kết nối")
        '    'MsgBox("Kết nối thành công")
        'End If
        DsXuatKho.Tables("sp_LoaiSanPham_Select_Phuc").Clear()
        clsSQL.FillDataSet("sp_LoaiSanPham_Select_Phuc", DsXuatKho, "sp_LoaiSanPham_Select_Phuc")
        DsXuatKho.Tables("sp_SanPham_Select_Phuc").Clear()
        clsSQL.FillDataSet("sp_SanPham_Select_Phuc", DsXuatKho, "sp_SanPham_Select_Phuc", 0)
        DsXuatKho.Tables("sp_Congty_Select_XuatKho").Clear()
        clsSQL.FillDataSet("sp_Congty_Select_XuatKho", DsXuatKho, "sp_Congty_Select_XuatKho")
        DsXuatKho.Tables("sp_Nguoimua_SelectBYMaCty_Xuatkho").Clear()
        clsSQL.FillDataSet("sp_Nguoimua_SelectBYMaCty_Xuatkho", DsXuatKho, "sp_Nguoimua_SelectBYMaCty_Xuatkho", 0)
        LoadCmbChietKhau()
        lblNguoiBan.Text = TenNVDangNhap
        tien = 0
        tienthanhtoan = 0
        hoahong = 0
        tiencon = 0
        tongtien = 0
        LaysoHD()
        DateTimePicker1.Value = Now
        ' MsgBox(DateTimePicker1.Value)
    End Sub
    Dim tongtien As Double
    Sub LaysoHD()
        clsSQL.ExecReader("sp_Hoadonxuat_LaySoHD")
        clsSQL.getData.Read()
        lblHDSo.Text = clsSQL.getData("SHD").ToString
        clsSQL.getData.Close()
    End Sub
    Sub LoadCmbChietKhau()
        Dim i As Integer
        For i = 0 To 100
            cmbChietKhau.Items.Add(i.ToString)
        Next
        cmbChietKhau.SelectedIndex = 0
        cmbVAT.SelectedIndex = 0
    End Sub

    Private Sub cmbLoaiSP_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbLoaiSP.SelectedIndexChanged

        If cmbDathang.Text = "Sửa" Then
            MsgBox("Đang cập nhật sản phẩm")

        Else

            DsXuatKho.Tables("sp_SanPham_Select_Phuc").Clear()
            clsSQL.FillDataSet("sp_SanPham_Select_Phuc", DsXuatKho, "sp_SanPham_Select_Phuc", cmbLoaiSP.SelectedValue)
            lblDVTinh.Text = "<null>"
            lblSoSeri.Text = "<null>"
            lblBaoHanh.Text = "<null>"
            lblTinhtrang.Text = "<null>"
            lblTon.Text = "<null>"
            txtDonGia.Text = ""
            txtSLBan.Text = ""
            cmbChietKhau.SelectedIndex = 0
            'lblTongtien.Text = "<null>"
        End If
    End Sub

    Private Sub cmbTenSP_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbTenSP.SelectedIndexChanged

        If cmbDathang.Text = "Sửa" Then
            MsgBox("Đang cập nhật sản phẩm")

        Else
            If cmbTenSP.SelectedIndex <> 0 Then
                clsSQL.ExecReader("sp_SanPham_SelectByMaSP_Phuc", cmbTenSP.SelectedValue)
                clsSQL.getData.Read()
                If clsSQL.getData.HasRows Then
                    lblDVTinh.Text = clsSQL.getData("DonViTinh").ToString
                    lblSoSeri.Text = clsSQL.getData("SoSeri").ToString
                    lblBaoHanh.Text = clsSQL.getData("ThoiGianBaoHanh").ToString
                    lblTinhtrang.Text = clsSQL.getData("TinhTrangMoi").ToString
                    lblTon.Text = clsSQL.getData("Soluongton").ToString
                    txtDonGia.Text = clsSQL.getData("Dongiaxuat").ToString
                    cmbChietKhau.SelectedIndex = 0
                    txtSLBan.Text = ""
                End If

            End If
        End If
    End Sub

    Private Sub cmbCongty_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbCongty.SelectedIndexChanged
        DsXuatKho.Tables("sp_Nguoimua_SelectBYMaCty_Xuatkho").Clear()
        clsSQL.FillDataSet("sp_Nguoimua_SelectBYMaCty_Xuatkho", DsXuatKho, "sp_Nguoimua_SelectBYMaCty_Xuatkho", cmbCongty.SelectedValue)
    End Sub

    Sub LoadHuy()
        DsXuatKho.Tables("sp_LoaiSanPham_Select_Phuc").Clear()
        clsSQL.FillDataSet("sp_LoaiSanPham_Select_Phuc", DsXuatKho, "sp_LoaiSanPham_Select_Phuc")
        DsXuatKho.Tables("sp_SanPham_Select_Phuc").Clear()
        clsSQL.FillDataSet("sp_SanPham_Select_Phuc", DsXuatKho, "sp_SanPham_Select_Phuc", cmbLoaiSP.SelectedValue)
        lblDVTinh.Text = "<null>"
        lblSoSeri.Text = "<null>"
        lblBaoHanh.Text = "<null>"
        lblTinhtrang.Text = "<null>"
        lblTon.Text = "<null>"
        txtDonGia.Text = ""
        txtSLBan.Text = ""
        cmbChietKhau.SelectedIndex = 0
        lblTongtien.Text = "<null>"


        tien = 0
        tienthanhtoan = 0
        hoahong = 0
        tiencon = 0
        tongtien = 0
        cmbNguoiMua.SelectedIndex = 0
        cmbCongty.SelectedIndex = 0
        cmbVAT.SelectedIndex = 0
        txtHinhthucThanhtoan.Text = ""
        txtTienthanhtoan.Text = "0"
        txtHoahong.Text = "0"
        lblTienCon.Text = "<null>"
        cmbDathang.Text = "Đặt"

        dtgXuat.Rows.Clear()
    End Sub
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        LoadHuy()
    End Sub
    Function tien_thanhtoan() As Double
        If txtTienthanhtoan.Text = "" Then
            txtTienthanhtoan.Text = "0"
        End If
        Return Double.Parse(txtTienthanhtoan.Text)
    End Function
    Function tien_hoahong() As Double
        If txtHoahong.Text = "" Then
            txtHoahong.Text = "0"
        End If
        Return Double.Parse(txtHoahong.Text)
    End Function

    Dim tien As Double
    Dim tienthanhtoan As Double
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbDathang.Click
        If cmbDathang.Text = "Sửa" Then 'cập nhật sản phẩm trong lưới
            If txtSLBan.Text = "" Then
                MsgBox("chưa nhập số lượng xuất")
            Else
                Dim row As Integer = dtgXuat.CurrentRow.Index
                tien = tien - Double.Parse(dtgXuat.Item(6, row).Value)
                tien += Double.Parse(txtDonGia.Text) * Integer.Parse(txtSLBan.Text) - Integer.Parse(cmbChietKhau.Text) * (Double.Parse(txtDonGia.Text) * Integer.Parse(txtSLBan.Text)) / 100
                tiencon = tien_thanhtoan() - tien_tongtien(cmbVAT.Text) + tien_hoahong()
                'tiencon = tien_thanhtoan() - tien - tien_hoahong()
                dtgXuat.Item(3, row).Value = txtSLBan.Text
                dtgXuat.Item(4, row).Value = txtDonGia.Text
                dtgXuat.Item(5, row).Value = cmbChietKhau.Text & "%"
                dtgXuat.Item(6, row).Value = Double.Parse(txtDonGia.Text) * Integer.Parse(txtSLBan.Text) - Integer.Parse(cmbChietKhau.Text) * (Double.Parse(txtDonGia.Text) * Integer.Parse(txtSLBan.Text)) / 100
                xuattien("tong") 'lblTongtien.Text = tien_tongtien(cmbVAT.Text).ToString("###,###,###")
                xuattien("con") 'lblTienCon.Text = tiencon.ToString("###,###,###")
                cmbDathang.Text = "Đặt"
            End If
        Else  ' thêm sản phẩm trong lưới
            If dtgXuat.RowCount = 0 Then
                If txtSLBan.Text = "" Then
                    MsgBox("chưa nhập số lượng xuất")
                Else

                    Dim slban As Integer = Integer.Parse(txtSLBan.Text)
                    Dim slton As Integer = Integer.Parse(lblTon.Text)
                    If slton > 0 Then
                        If slban <= slton Then
                            tien += Double.Parse(txtDonGia.Text) * Integer.Parse(txtSLBan.Text) - Integer.Parse(cmbChietKhau.Text) * (Double.Parse(txtDonGia.Text) * Integer.Parse(txtSLBan.Text)) / 100
                            tiencon = tien_thanhtoan() - tien_tongtien(cmbVAT.Text) + tien_hoahong()
                            'tiencon = tien_thanhtoan() - tien - tien_hoahong()
                            dtgXuat.Rows.Add(cmbLoaiSP.Text, cmbTenSP.SelectedValue.ToString, cmbTenSP.Text, txtSLBan.Text, txtDonGia.Text, cmbChietKhau.Text & "%", Double.Parse(txtDonGia.Text) * Integer.Parse(txtSLBan.Text) - Integer.Parse(cmbChietKhau.Text) * (Double.Parse(txtDonGia.Text) * Integer.Parse(txtSLBan.Text)) / 100)
                            'lblTongtien.Text = tien_tongtien(cmbVAT.Text).ToString("###,###,###") 'tien.ToString("###,###,###")
                            xuattien("tong") 'lblTongtien.Text = tien_tongtien(cmbVAT.Text).ToString("###,###,###")
                            xuattien("con") 'lblTienCon.Text = tiencon.ToString("###,###,###")
                        Else
                            MsgBox("Kiểm tra số lượng bán")
                        End If
                    Else
                        MsgBox("Sản phẩm hiện đã hết")

                    End If
                End If
            Else
                Dim i As Integer = 0
                For i = 0 To dtgXuat.RowCount - 1
                    If cmbTenSP.SelectedValue.ToString = dtgXuat.Item(1, i).Value.ToString Then
                        MsgBox("Đã chọn sản phẩm này ", MsgBoxStyle.Information, "Thông Báo")
                        GoTo here
                    End If
                Next
                tien += Double.Parse(txtDonGia.Text) * Integer.Parse(txtSLBan.Text) - Integer.Parse(cmbChietKhau.Text) * (Double.Parse(txtDonGia.Text) * Integer.Parse(txtSLBan.Text)) / 100

                tiencon = tien_thanhtoan() - tien_tongtien(cmbVAT.Text) + tien_hoahong()
                'tiencon = tien_thanhtoan() - tien - tien_hoahong()

                dtgXuat.Rows.Add(cmbLoaiSP.Text, cmbTenSP.SelectedValue.ToString, cmbTenSP.Text, txtSLBan.Text, txtDonGia.Text, cmbChietKhau.Text & "%", Double.Parse(txtDonGia.Text) * Integer.Parse(txtSLBan.Text) - Integer.Parse(cmbChietKhau.Text) * (Double.Parse(txtDonGia.Text) * Integer.Parse(txtSLBan.Text)) / 100)
                xuattien("tong") 'lblTongtien.Text = tien_tongtien(cmbVAT.Text).ToString("###,###,###")
                xuattien("con") 'lblTienCon.Text = tiencon.ToString("###,###,###")
            End If
here:
        End If
    End Sub

    Function tien_tongtien(ByVal vat As String) As Double
        Return tien + tien * Double.Parse(vat.Replace("%", "")) / 100
    End Function
    Sub xuattien(ByVal loai As String)
        If loai = "tong" Then
            If tien_tongtien(cmbVAT.Text) = 0 Then
                lblTongtien.Text = "0"
            Else
                lblTongtien.Text = tien_tongtien(cmbVAT.Text).ToString("###,###,###")
            End If
        ElseIf loai = "con" Then
            If tiencon = 0 Then
                lblTienCon.Text = "0"
            Else
                lblTienCon.Text = tiencon.ToString("###,###,###")
            End If
        End If
    End Sub
    Private Sub cmbVAT_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbVAT.SelectedIndexChanged

        'lblTongtien.Text = tien_tongtien(cmbVAT.Text).ToString("###,###,###")
        xuattien("tong")
        tiencon = tien_thanhtoan() - tien_tongtien(cmbVAT.Text) + tien_hoahong()
        'lblTienCon.Text = tiencon.ToString("###,###,###")
        xuattien("con")
        'If cmbVAT.Text = "10%" Then
        '    tongtien = tien + tien * 10 / 100
        '    lblTongtien.Text = tongtien.ToString("###,###,###")
        '    '' tiencon = tien_thanhtoan() - tien - tien_hoahong()
        '    'lblTienCon.Text = tiencon.ToString("###,###,###")
        'Else
        '    tongtien = tien
        '    lblTongtien.Text = tongtien.ToString("###,###,###")
        '    ''tiencon = tien_thanhtoan() - tien - tien_hoahong()
        '    'lblTienCon.Text = tiencon.ToString("###,###,###")
        'End If
    End Sub



    Private Sub dtgXuat_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles dtgXuat.DoubleClick
        txtSLBan.Text = Integer.Parse(dtgXuat.Item(3, dtgXuat.CurrentRow.Index).Value)
        cmbLoaiSP.Text = dtgXuat.Item(0, dtgXuat.CurrentRow.Index).Value.ToString
        cmbTenSP.Text = dtgXuat.Item(2, dtgXuat.CurrentRow.Index).Value.ToString
        cmbChietKhau.Text = dtgXuat.Item(5, dtgXuat.CurrentRow.Index).Value.ToString.Replace("%", "")
        cmbDathang.Text = "Sửa"
    End Sub

    Private Sub txtSLBan_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtSLBan.KeyPress
        If Char.IsDigit(e.KeyChar) = False And Char.IsControl(e.KeyChar) = False Then
            e.Handled = True
        End If
    End Sub

    Private Sub txtTienthanhtoan_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtTienthanhtoan.KeyDown
        If e.KeyCode = Keys.Enter Then
            If txtTienthanhtoan.Text = "" Then
                txtTienthanhtoan.Text = "0"
            Else

                'tienthanhtoan = Double.Parse(txtTienthanhtoan.Text)
                tiencon = tien_thanhtoan() - tien_tongtien(cmbVAT.Text) + tien_hoahong()
                'tiencon = tien_thanhtoan() - tien - tien_hoahong()
                xuattien("con") 'lblTienCon.Text = tiencon.ToString("###,###,###")

                txtTienthanhtoan.Text = tien_thanhtoan().ToString("###,###,###")
            End If
        End If
    End Sub

    Private Sub txtTienthanhtoan_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtTienthanhtoan.KeyPress
        If Char.IsDigit(e.KeyChar) = False And Char.IsControl(e.KeyChar) = False Then
            e.Handled = True
        End If
    End Sub


    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        If cmbNguoiMua.SelectedValue > 0 Then
            If dtgXuat.RowCount > 0 Then
                'MsgBox("mã hóa đơn " & Integer.Parse(lblHDSo.Text).ToString & " - ngày lập " & DateTimePicker1.Value.ToString("dd/MM/yyyy") & " - hình thức thanh toán " & txtHinhthucThanhtoan.Text & " - tiền thanh toán " & tien_thanhtoan.ToString & " - tiền còn " & tiencon.ToString & " - hoa hồng " & tien_hoahong.ToString & " - vat " & Integer.Parse(cmbVAT.Text.Replace("%", "")).ToString & " - mã người mua " & cmbNguoiMua.SelectedValue.ToString & " - user " & lblNguoiBan.Text)
                Dim mahd As Integer = Integer.Parse(lblHDSo.Text)
                Dim nlap = DateTimePicker1.Value.ToString("dd/MM/yyyy")
                Dim htttoan As String = txtHinhthucThanhtoan.Text
                Dim tien_ttoan As Double = tien_thanhtoan()
                Dim tien_con As Double = tiencon
                Dim hoahong As Double = tien_hoahong()
                Dim thuevat As Integer = Integer.Parse(cmbVAT.Text.Replace("%", ""))
                Dim manguoimua As Integer = cmbNguoiMua.SelectedValue
                Dim user As String = lblNguoiBan.Text
                'clsSQL.ExecCommand("sp_Hoadonxua_InsertHDX", Integer.Parse(lblHDSo.Text), DateTimePicker1.Value.ToString("dd/MM/yyyy"), txtHinhthucThanhtoan.Text, tienthanhtoan, tiencon, hoahong, Integer.Parse(cmbVAT.Text.Replace("%","")), cmbNguoiMua.SelectedValue, lblNguoiBan.Text)
                clsSQL.ExecCommand("sp_Hoadonxua_InsertHDX", mahd, nlap, htttoan, tien_ttoan, tien_con, hoahong, thuevat, manguoimua, user)
                Dim i As Integer = 0

                For i = 0 To dtgXuat.Rows.Count - 1
                    'MsgBox(" mã hóa đơn bán " & Integer.Parse(lblHDSo.Text).ToString & " - mã sản phẩm " & dtgXuat.Item(1, dtgXuat.Rows(i).Index).Value.ToString & " - số lượng " & Integer.Parse(dtgXuat.Item(3, dtgXuat.Rows(i).Index).Value).ToString & " - đơn giá " & Double.Parse(dtgXuat.Item(4, dtgXuat.Rows(i).Index).Value).ToString & " - chiết khấu " & Integer.Parse(dtgXuat.Item(5, dtgXuat.Rows(i).Index).Value.ToString.Replace("%", "")).ToString)
                    Dim msp As Integer = dtgXuat.Item(1, dtgXuat.Rows(i).Index).Value
                    Dim sl As Integer = Integer.Parse(dtgXuat.Item(3, dtgXuat.Rows(i).Index).Value)
                    Dim dg As Double = Double.Parse(dtgXuat.Item(4, dtgXuat.Rows(i).Index).Value)
                    Dim chietkhau As Integer = Integer.Parse(dtgXuat.Item(5, dtgXuat.Rows(i).Index).Value.ToString.Replace("%", ""))
                    '    clsSQL.ExecCommand("sp_Chitiethoadonxuat_InsertCTHDX", Integer.Parse(lblHDSo.Text), dtgXuat.Item(1, dtgXuat.Rows(i).Index).Value, Integer.Parse(dtgXuat.Item(3, dtgXuat.Rows(i).Index).Value), Double.Parse(dtgXuat.Item(4, dtgXuat.Rows(i).Index).Value), Integer.Parse(dtgXuat.Item(5, dtgXuat.Rows(i).Index).Value.ToString.Replace("%", "")))
                    clsSQL.ExecCommand("sp_Chitiethoadonxuat_InsertCTHDX", mahd, msp, sl, dg, chietkhau)
                Next
                LoadHuy()
                LaysoHD()
                MsgBox("Đã lưu thành công")
            Else
                MsgBox("Không có dữ liệu để lưu")
            End If
        Else
            MsgBox("Chưa chọn khách hàng")
        End If
    End Sub

    Private Sub txtHoahong_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtHoahong.KeyDown
        If e.KeyCode = Keys.Enter Then
            If txtHoahong.Text = "" Then
                txtHoahong.Text = "0"
            Else

                ' hoahong = Double.Parse(txtHoahong.Text)
                'tiencon = tien_thanhtoan() - tien - tien_hoahong()
                tiencon = tien_thanhtoan() - tien_tongtien(cmbVAT.Text) + tien_hoahong()
                txtHoahong.Text = tien_hoahong().ToString("###,###,###")
                xuattien("con") 'lblTienCon.Text = tiencon.ToString("###,###,###")
            End If
        End If
    End Sub
    Dim hoahong As Double
    Dim tiencon As Double
    Private Sub txtHoahong_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtHoahong.KeyPress
        If Char.IsDigit(e.KeyChar) = False And Char.IsControl(e.KeyChar) = False Then
            e.Handled = True
        End If
    End Sub

    Private Sub mnuXoa_Opening(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles mnuXoa.Opening
        If dtgXuat.RowCount > 0 Then
            mniXoaDong.Enabled = True
        Else
            mniXoaDong.Enabled = False
        End If
    End Sub

   
    Private Sub mniXoaDong_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mniXoaDong.Click
        Dim row As Integer = dtgXuat.CurrentRow.Index

        tien = tien - Double.Parse(dtgXuat.Item(6, row).Value)
        dtgXuat.Rows.RemoveAt(row)
        xuattien("tong") 'lblTongtien.Text = tien_tongtien(cmbVAT.Text).ToString("###,###,###")
        tiencon = tien_thanhtoan() - tien_tongtien(cmbVAT.Text) + tien_hoahong()

        xuattien("con") 'lblTienCon.Text = tiencon.ToString("###,###,###")
    End Sub

    Private Sub bttnThemNguoimua_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bttnThemNguoimua.Click
        FrmCongtyKH.ShowDialog()

    End Sub
End Class